package com.dhec.ptms.epr.db;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dhec.ptms.epr.model.ApparatusPrecision;
import com.ionglobal.db.DBI;
import com.ionglobal.lib.SysException;

public class ApparatusPrecisionDB {
	private static final Logger logger = LoggerFactory.getLogger(ApparatusPrecisionDB.class);
	
	public List<ApparatusPrecision> list(DBI dbi, int page, int pagesize){
		ArrayList<ApparatusPrecision> list = new ArrayList<ApparatusPrecision>(0);
		try {
			dbi.skipToPage(pagesize, page, "select * from EPR_ApparatusPrecision order by id desc");
			while(dbi.rsNext()){
				ApparatusPrecision bean = new ApparatusPrecision();
				this.handleData(dbi, bean);
				list.add(bean);
			}
			dbi.rsClose();
		} catch (SysException e) {
			logger.error("ApparatusPrecisionDB.list(DBI) Error!", e);
		}
		
		return list;
		
	}
	
	public void handleData(DBI dbi, ApparatusPrecision bean) throws SysException{
		bean.setCreatetime(dbi.rsGetDate("createtime"));
		bean.setCreateuser(dbi.rsGetString("createuser"));
		bean.setEnabled(dbi.rsGetInt("enabled")==1);
		bean.setId(dbi.rsGetInt("id"));
		bean.setProcessdata(dbi.rsGetString("processdata"));
		bean.setRoom(dbi.rsGetString("room"));
		bean.setStandard(dbi.rsGetString("standard"));
		bean.setUpdatetime(dbi.rsGetDate("updatetime"));
		bean.setUpdateuser(dbi.rsGetString("updateuser"));
		bean.setTjmodel(dbi.rsGetString("tjmodel"));
	}
	
	public long count(DBI dbi){
		long c = 0;
		try {
			dbi.execQuery("select count(*) from EPR_ApparatusPrecision");
			if(dbi.rsNext()){
				c = dbi.rsGetLong(1);
			}
			dbi.rsClose();
		} catch (SysException e) {
			logger.error("ApparatusPrecisionDB.list(DBI) Error!", e);
		}
		
		return c;
		
	}
	
	public boolean save(DBI dbi, ApparatusPrecision bean){
		try {
			return dbi.execute("insert into EPR_ApparatusPrecision (id, room, standard, "
					+ "processdata, createuser, createtime, updateuser, updatetime, "
					+ "enabled,tjmodel) values("+bean.getId()+","
					+ "'"+dbi.fixSql(bean.getRoom())+"',"
					+ "'"+dbi.fixSql(bean.getStandard())+"',"
					+ "'"+dbi.fixSql(bean.getProcessdata())+"',"
					+ "'"+dbi.fixSql(bean.getCreateuser())+"',"
					+ ""+dbi.getCurrentDateTime()+","
					+ "'"+dbi.fixSql(bean.getUpdateuser())+"',"
					+ ""+dbi.getCurrentDateTime()
					+","+(bean.getEnabled() ? "1": "0")+",'"+dbi.fixSql(bean.getTjmodel())+"')");
		} catch (SysException e) {
			logger.error("ApparatusPrecisionDB.save(DBI, ApparatusPrecision) Error!", e);
		}
		
		return false;
		
	}
	
	public ApparatusPrecision find(DBI dbi, int id){
		ApparatusPrecision bean = null;
		try {
			dbi.execQuery("select * from EPR_ApparatusPrecision where id="+id);
			if(dbi.rsNext()){
				bean = new ApparatusPrecision();
				this.handleData(dbi, bean);
			}
			dbi.rsClose();
		} catch (SysException e) {
			logger.error("ApparatusPrecisionDB.find(DBI, int) Error!", e);
		}
		
		return bean;
		
	}
	
	public boolean update(DBI dbi, ApparatusPrecision bean){
		try {
			return dbi.execute("update EPR_ApparatusPrecision set room='"+dbi.fixSql(bean.getRoom())+"',"
					+ "processdata='"+dbi.fixSql(bean.getProcessdata())+"',"
					+ "standard='"+dbi.fixSql(bean.getStandard())+"',"
					+ "tjmodel='"+dbi.fixSql(bean.getTjmodel())+"',"
					+ "updateuser='"+dbi.fixSql(bean.getUpdateuser())+"',"
					+ "updatetime="+dbi.getCurrentDateTime()+","
					+ "enabled="+(bean.getEnabled()?"1":"0")+" where id="+bean.getId());
		} catch (SysException e) {
			logger.error("ApparatusPrecisionDB.update(DBI, ApparatusPrecision) Error!", e);
		}
		
		return false;
		
	}
	
	public boolean delete(DBI dbi, int id){
		try {
			return dbi.execute("delete from EPR_ApparatusPrecision where id="+id);
		} catch (SysException e) {
			logger.error("ApparatusPrecisionDB.delete(DBI, ApparatusPrecision) Error!", e);
		}
		
		return false;
		
	}
}
